package com.vim.modules.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vim.common.annotation.SysLogRecord;
import com.vim.common.model.response.Response;
import com.vim.common.model.response.RestfulResponse;
import com.vim.common.model.response.TableListResponse;
import com.vim.common.utils.ValidateUtils;
import com.vim.modules.web.model.SysConfig;
import com.vim.modules.web.service.SysConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/sysConfig")
public class SysConfigController {

	@Resource
	private SysConfigService sysConfigService;

	/**
	 * 列表查询
     * @param page 页码
     * @param limit 每页显示数量
	 */
	@RequestMapping("/list")
    public TableListResponse list(SysConfig sysConfig, @RequestParam(value = "page") Integer page, @RequestParam(value = "limit") Integer limit){
        ValidateUtils.checkNotZero(page);
        ValidateUtils.checkNotZero(limit);
	    PageHelper.startPage(page,limit);
        List<SysConfig> list = sysConfigService.findList(sysConfig);
        PageInfo<SysConfig> pageInfo = new PageInfo<>(list);
        return new TableListResponse(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 编辑
     */
    @SysLogRecord
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public RestfulResponse edit(SysConfig sysConfig){
        if(StringUtils.isNoneBlank(sysConfig.getConfigId())){
            sysConfigService.update(sysConfig);
        }else{
            sysConfigService.save(sysConfig);
        }
        return new RestfulResponse(Response.OK);
    }

    /**
     * 详情
     * @param id 主键ID
     */
    @RequestMapping("/detail")
    public RestfulResponse detail(@RequestParam(value = "id") String id){
        ValidateUtils.checkNotBlank(id);
        return new RestfulResponse(sysConfigService.find(id));
    }

    /**
     * 删除
     * @param id 主键ID
     */
    @SysLogRecord
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public RestfulResponse del(@RequestParam(value = "id") String id){
        ValidateUtils.checkNotBlank(id);
        sysConfigService.delete(id);
        return new RestfulResponse(Response.OK);
    }

}

